package dao;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 反射
 * @author HXZY
 *
 */
public class BaseDao<T>{
	private Class<T> reflectionClass;
	private String tableName;
	private Field[] field;
	public BaseDao() {
		ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
		reflectionClass=(Class<T>) type.getActualTypeArguments()[0];
		tableName=reflectionClass.getSimpleName();
		field=reflectionClass.getDeclaredFields();
	}
	/**
	 * 插入记录
	 * @param o
	 */
	public void insert(T o){
		String sql="insert into "+tableName+"(";
		for(int i=0;i<field.length;i++){
			if(i<field.length-1)
			sql+=field[i].getName()+",";
			else
				sql+=field[i].getName()+") values(";
		}
		for(int i=0;i<field.length;i++){
			if(i<field.length-1)
			sql+="?,";
			else                                                                   
				sql+="?)";
		}
		System.out.println(sql);
		Connection con=Util.getConnection();
		try {
			con.setAutoCommit(false);		//执行后先不提交事务
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			PreparedStatement pstm=con.prepareStatement(sql);
			for(int i=0;i<field.length;i++){
				field[i].setAccessible(true);
				pstm.setObject(i+1, field[i].get(o));
			}
			pstm.executeUpdate();
		} catch(SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			con.commit();					//提交事务
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 查询记录 
	 * @return
	 */
	public List<T> findAll(){
		List<T> list=new ArrayList<>();
		String sql="select * from "+tableName;
		Connection con=Util.getConnection();
		try {
			Statement stm=con.createStatement();
			ResultSet rs=stm.executeQuery(sql);
			while(rs.next()){				
				T bd=reflectionClass.newInstance();//通过类类型创建该类对象
				for(int i=0;i<field.length;i++){
					Object values=rs.getObject(i+1);	//获取字段名
					if(values instanceof BigDecimal){//类型转换
						BigDecimal bdl=(BigDecimal) values;
						values=bdl.intValue();
					}
					field[i].setAccessible(true);
					field[i].set(bd, values);
				}
				list.add(bd);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 删除记录
	 * @param id
	 */
	public void delete(String sql/*,Object id*/){
		//String sql="delete from "+tableName+" where "+field[0].getName()+" = ?";
		Connection con=Util.getConnection();
		try {
			PreparedStatement pstm=con.prepareStatement(sql);
			//pstm.setObject(1, id);
			pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 修改记录
	 * @param dname
	 * @param id
	 */
	public void update(Object dname,Object id){
		String sql="update "+tableName+" set "+field[1].getName()+" = ?"+" where "+field[0].getName()+" = ?";
		Connection con=Util.getConnection();
		try {
			PreparedStatement pstm=con.prepareStatement(sql);
			pstm.setObject(1,dname);
			pstm.setObject(2,id);
			pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
